package com.hdax.service.Impl;

import com.hdax.entity.Attendance;
import com.hdax.mappers.AttendanceMapper;
import com.hdax.service.AttendanceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class AttendanceServiceImpl implements AttendanceService{

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public List<Attendance> findAll(int offset, int limit, Attendance attendance) {
        return attendanceMapper.findAll(offset,limit,attendance);
    }

    @Override
    public int count(Attendance attendance) {
        return attendanceMapper.count(attendance);
    }

    @Override
    public int insertByshang(Attendance attendance) {
        //获取当前打卡时间
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        //当前打卡时间
        String dangqian=formatter.format(date);
        attendance.setDakadate(dangqian);
        attendance.setEstablishtime(dangqian);

        //判断打卡状态
        //获取当前打卡时间
        SimpleDateFormat formatter1= new SimpleDateFormat("HH:mm:ss");
        Date date1 = new Date(System.currentTimeMillis());
        String shangdak= String.valueOf(date1);
        attendance.setShangbantime(formatter1.format(date1));
        String skaimo="09:30:00";
        String xkaimo="11:00:00";
        if (shangdak.compareTo(skaimo)<0){//打卡时间在规定时间之前
            attendance.setState(4);//正常
        }else if(shangdak.compareTo(skaimo)==0){
            attendance.setState(4);//正常
        }else if(shangdak.compareTo(skaimo)>0 && shangdak.compareTo(xkaimo)<0){
            attendance.setState(1);//迟到
        }else if(shangdak.compareTo(xkaimo)>0){
            attendance.setState(3);//旷工
        }
        return attendanceMapper.insertByshang(attendance);
    }

    @Override
    public int updateByxia(Attendance attendance) {

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        attendance.setDakadateone(formatter.format(date));
        Attendance a=ac(attendance);
        int astate=a.getState();//获取下班打卡的状态
        attendance.setId(a.getId());
        //判断打卡状态
        //获取当前打卡时间
        SimpleDateFormat formatter1= new SimpleDateFormat("HH:mm:ss");
        Date date1 = new Date(System.currentTimeMillis());
        String xiaban= String.valueOf(date1);
        attendance.setXiabantime(formatter1.format(date1));
        String skaimo="17:00:00";
        String xkaimo="15:00:00";
        if(xiaban.compareTo(xkaimo)<0){
            attendance.setState(3);//旷工
        }else if (xiaban.compareTo(skaimo)>0){//打卡时间在规定时间之前
            if (astate==4){
                attendance.setState(4);//正常
            }else{
                attendance.setState(astate);
            }
        }else if (xiaban.compareTo(xkaimo)>0 && xiaban.compareTo(skaimo)<0){
            if (astate==4){
                attendance.setState(1);//正常
            }else{
                attendance.setState(astate);
            }
        }
        return attendanceMapper.updateByxia(attendance);
    }

    @Override
    public int deleteByid(int id) {
        return attendanceMapper.deleteByid(id);
    }

    @Override
    public List<Attendance> CountMoney() {
        return attendanceMapper.CountMoney();
    }

    @Override
    public int countByAtt(Attendance attendance) {
        return attendanceMapper.countByAtt(attendance);
    }

    @Override
    public int CountState(Attendance attendance,int state) {
        return attendanceMapper.CountState(attendance,state);
    }

    @Override
    public Attendance ac(Attendance attendance) {
        return attendanceMapper.ac(attendance);
    }

    @Override
    public int insetByDate(int employeeid, String dakadate, String establishtime) {
        return attendanceMapper.insetByDate(employeeid,dakadate,establishtime);
    }

    @Override
    public int selectByDaka(Attendance attendance) {
        return attendanceMapper.selectByDaka(attendance);
    }

    @Override
    public Attendance selectByid(int id) {
        return attendanceMapper.selectByid(id);
    }

    @Override
    public List<Attendance> findByeid(int employeeid) {
        return attendanceMapper.findByeid(employeeid);
    }

    @Override
    public Attendance setexist(int employeeid, String dakadate, String dakadateone) {
        return attendanceMapper.setexist(employeeid,dakadate,dakadateone);
    }

    @Override
    public int setsan(int employeeid, String dakadate) {
        return attendanceMapper.setsan(employeeid,dakadate);
    }
}
